package org.finalministry.database.dao;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.finalministry.database.connection.IDatabaseConnection;

public class DeveloperDAO extends BaseDAO {

	public DeveloperDAO(IDatabaseConnection connection) {
		super(connection);
	}

	/**
	 * A dangerous method, which should be enabled only in developer mode
	 * 
	 * @param sql
	 * @return
	 * @throws DataAccessLayerException
	 */

	@SuppressWarnings("unchecked")
	public List<Object> executeSQL(String sql) {

		List<Object> results = new ArrayList<Object>();

		try {

			Statement stmt = getConnection().getConnection().createStatement();
			ResultSet result = stmt.executeQuery(sql);

			int columnCount = result.getMetaData().getColumnCount();

			while (result.next()) {

				if (columnCount == 1) {
					results.add(result.getString(1));
				}

				Object[] object = new Object[columnCount];

				for (int x = 0; x < columnCount; x++) {

					object[x] = result.getObject(x + 1);

				}

				results.add(object);
			}

			return results;
		} catch (Exception e) {
			e.printStackTrace();
			return results;
		}

	}

}
